package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Animal;
import model.Command;
import model.IAction;
import model.Position;
import service.GlobalService;
import utilities.CRes;
import utilities.GameResource;
import utilities.TileMap;
import animation.Smoke;

public class HydridsAnimalScr extends Screen{

	static HydridsAnimalScr instance;
	private Image m_imgBackground;
	private Vector m_items = null;
	private Position[] m_arrPos = new Position[2];
	 
	boolean m_bIsShowDlg = false;
	boolean m_bIsAction = false;
	boolean m_bIsWin = false;
	boolean m_bCompleteAction = false;
	String m_strAnimal = "";
	Command center, right;
	int deltaW, m_iWAnimal;
	int speed;
	
	public static HydridsAnimalScr gI(){
		if(instance == null){
			instance = new HydridsAnimalScr();
		}
		return instance;
	}
	
	public HydridsAnimalScr(){
		m_cmdLeft = new Command(GameResource.menu, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();
                menu.addElement(m_viewMsgCmd);
                GameCanvas.menu.startAt(menu, 0);
            }
    	});
    	
    	
    	center = new Command(GameResource.select, new IAction() {
			
    		public void perform() {
				// TODO Auto-generated method stub
			}
		});
    	
    	right = new Command(GameResource.exit, new IAction() {			
			public void perform() {
				// TODO Auto-generated method stub
				close();
			}
		});
		
		
		
		m_arrPos[0] = new Position(10, GameCanvas.h - 50);
		m_arrPos[0].anchor = 0;
		
		m_arrPos[1] = new Position(GameCanvas.w - 50, m_arrPos[0].y);
		m_arrPos[1].anchor = 1;
		m_bIsShowDlg = false;
		m_bIsAction = false;
		m_bCompleteAction = false;
		TileMap.instance.loadMap(TileMap.LOGIN_MAP);
        
        //draw background to image buffer
        int height = GameResource.instance.imgScreenLogin.getHeight()+TileMap.m_iHeightTile*3;
        m_imgBackground = Image.createImage(GameCanvas.w, height);
 
        Graphics g = m_imgBackground.getGraphics();
        g.setColor(0x8be2ff);
        g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
        int i = 0;
        int tmpW = GameResource.instance.imgScreenLogin.getWidth();
        for(i = 0; i < GameCanvas.w; i += tmpW){
        	g.drawImage(GameResource.instance.imgScreenLogin, i, 20, Graphics.LEFT | Graphics.TOP);
        }        
        TileMap.instance.paintMap(g, 0, GameResource.instance.imgScreenLogin.getHeight());
        int wMap = TileMap.instance.m_gardenMap.m_iMapWidth * TileMap.m_iWidthTile;
        if(wMap < GameCanvas.w){
        	//adding tile for large screen
        	tmpW = (GameCanvas.w - wMap)/TileMap.m_iWidthTile + 1;
        	TileMap.instance.paintMap(g, wMap, GameResource.instance.imgScreenLogin.getHeight(), tmpW, TileMap.instance.m_gardenMap.m_iMapHeight);
        }
        
        g.drawImage(GameResource.instance.imgTree, GameCanvas.hw +50, GameResource.instance.imgScreenLogin.getHeight() - 38, Graphics.LEFT | Graphics.TOP);
        g.drawImage(GameResource.instance.imgTree, GameCanvas.hw - 80, GameResource.instance.imgScreenLogin.getHeight() - 35, Graphics.LEFT | Graphics.TOP);
                
        int tmp = GameCanvas.w/3;
        tmpW = GameResource.instance.imgFance.getWidth();
        for(i = 0; i < tmp; i += tmpW){
        	g.drawImage(GameResource.instance.imgFance, i, GameResource.instance.imgScreenLogin.getHeight() - 10, Graphics.LEFT | Graphics.TOP);
        }
        tmp = GameCanvas.w2d3;
        for(i = GameCanvas.w; i > tmp; i -= tmpW){
        	g.drawImage(GameResource.instance.imgFance, i, GameResource.instance.imgScreenLogin.getHeight() - 10, Graphics.RIGHT | Graphics.TOP);
        }
	}
	
	public void switchToMe(){
		super.switchToMe();
		GameCanvas.endDlg();
    	System.gc();
	}
	
	public void setListItem(Vector items) {
		if(m_items != null){
			m_items = null;
		}
		m_items = new Vector();
		for(int i = 0; i < items.size(); i ++){
			Animal ani = (Animal)items.elementAt(i);
			Animal newAni = new Animal(ani.animalInfo.m_iId);
			ani.copyTo(newAni);
			m_items.addElement(newAni);
		}
		speed = 2;
		deltaW = 0;
		
		m_arrPos[0].x = 10;
		m_arrPos[0].y = GameCanvas.h - 50;
		m_arrPos[0].anchor = 0;
		
		m_arrPos[1].x = GameCanvas.w - 50;
		m_arrPos[1].y = m_arrPos[0].y; 
		m_arrPos[1].anchor = 1;
		
		m_bIsShowDlg = true;
		m_bIsWin = false;
		m_bCompleteAction = false;
		Animal ani = (Animal)items.elementAt(0);
		m_iWAnimal = ani.animalInfo.m_iW[1];
	
		m_bIsAction = true;
		m_cmdCenter = null;
		m_cmdRight = null;
    }
	
	public void paint(Graphics g){
		g.setClip(GameCanvas.hw - deltaW, 0, deltaW*2, GameCanvas.h);
		g.setColor(0x8be2ff);
    	g.fillRect(0, 0, GameCanvas.w, GameCanvas.h);
    	
        //draw background        
        g.drawImage(m_imgBackground, 0, GameCanvas.h - m_imgBackground.getHeight() - GameCanvas.hBottomBar, 0);
		
        if(!m_bCompleteAction){
			int i;
			for(i = 0; i < m_items.size(); i ++){
				Animal ani = (Animal)m_items.elementAt(i);
				if(!m_bIsAction)
					ani.paintAction(g, 1, Animal.STAND, m_arrPos[i].x, m_arrPos[i].y, m_arrPos[i].anchor );
				else
					ani.paintAction(g, 1, Animal.RUN, m_arrPos[i].x, m_arrPos[i].y, m_arrPos[i].anchor );
			}
        }
        else{
        	Animal ani = (Animal)m_items.elementAt(0);
        	if(m_bIsWin)
        		ani.paintAction(g, 1, Animal.RUN, m_arrPos[0].x, m_arrPos[0].y, m_arrPos[0].anchor );
        	else if(!m_bIsShowDlg){
        		ani.paintAction(g, 1, Animal.STAND, m_arrPos[0].x, m_arrPos[0].y, m_arrPos[0].anchor );
        	}
        	else
        		ani.paintAction(g, 1, Animal.SLEEP, m_arrPos[0].x, m_arrPos[0].y, m_arrPos[0].anchor );
        }
        
		super.paint(g);
	}
	
	public void clearData(){
  		m_items.removeAllElements();
    }
	
	public void close() {
		// TODO Auto-generated method stub
		GlobalService.gI().getGardenData(NuoiThuMidlet.m_myPlayerInfo.m_iId);
		
	}

	public void update() {
		// TODO Auto-generated method stub
		if(deltaW <= GameCanvas.hw){
			deltaW += 20;
		}
		
		if(m_bIsAction){
			if(((m_arrPos[0].x + m_iWAnimal) - (m_arrPos[1].x - m_iWAnimal)) >= 0){
				m_bIsAction = false;
				m_bCompleteAction = true;
				int i;
				for(i = 0; i < 10; i++){
					int a = CRes.random(2);
					int b = CRes.random(2);
					if(a == 0 && a == b)
						a = 1;
					byte c = 3;
					if(i>4)
						c = 2;
					if(i>7)
						c = 1;
					Smoke stun = new Smoke();
					stun.startEffect(m_arrPos[0].x+ m_iWAnimal, GameCanvas.h-70, a, b, c, Smoke.STAR);
				}
				Smoke stun = new Smoke();
				stun.startEffect(m_arrPos[0].x+m_iWAnimal , GameCanvas.h-70, 0, 0, (byte) 2, Smoke.STUN);
				m_cmdCenter = center;
				if(m_bIsWin){
					GameCanvas.startOK("Chúc mừng bạn đã có được con thú: "+m_strAnimal+"!", new IAction() {
						
						public void perform() {
							// TODO Auto-generated method stub
							close();
							GameCanvas.endDlg();
						}
					});
				}
				else{
					GameCanvas.startOK("Quá trình lai thú thất bại, bạn đã bị mất đi con thú: "+m_strAnimal+"!", new IAction() {
						
						public void perform() {
							// TODO Auto-generated method stub
							close();
							GameCanvas.endDlg();
						}
					});
				}
			}
			else{
				m_arrPos[0].x += speed;
				m_arrPos[1].x -= speed;
			}
		}
		if(!m_bIsAction && m_bCompleteAction && m_bIsWin){
			if(m_arrPos[0].x < (GameCanvas.w - 50))
				m_arrPos[0].x += speed;
			else{
				m_bIsShowDlg = false;
				m_bIsWin = false;
			}
		}
	}
	
	public void updateKey(){
		super.updateKey();
	}

	public void onCompleteHydrid(boolean result, short animalType,
			short animalId, byte bonusHealth, byte gender) {
		// TODO Auto-generated method stub
		int i;
		System.out.println(result+" "+animalType+" "+animalId+" "+bonusHealth+" "+gender);
		m_bIsWin = result;
		if(m_bIsWin){
			Animal ani = new Animal(animalType);
			ani.m_iId = animalId;
			m_strAnimal = ani.animalInfo.m_strName + " " + ani.m_iId;
		}
		else{
			for(i = 0 ; i < m_items.size(); i ++){
				Animal ani = (Animal)m_items.elementAt(i);
				if(animalId != ani.m_iId){
					m_strAnimal = ani.animalInfo.m_strName + " " + ani.m_iId;
					break;
				}
			}
		}
	}
}
